'''
Created on Aug 30, 2012

@author: kingsfield
'''
import sys
from collections import defaultdict
from util import Util
from datetime import datetime

#trainfile = '/home/oozie/huangyufan/kaggle/train.csv'
trainfile = '/home/kingsfield/Desktop/BestBuyPc/old_train.csv'
filename = '/home/kingsfield/Desktop/BestBuyPc/out/bigram_boost_bayes_origin_2.csv'
#filename = '/home/kingsfield/Desktop/BestBuyPc/out/bigram_boost_bayes.csv'
difffile = '/home/kingsfield/Desktop/BestBuyPc/diff.csv'
#filename = '/home/kingsfield/Desktop/BestBuyPc/out/random_naive_bayes.csv'

def get_date(click_time):
    try:
        t = datetime.strptime(click_time, '"%Y-%m-%d %H:%M:%S.%f"')
    except:
        t = datetime.strptime(click_time, '"%Y-%m-%d %H:%M:%S"')
    return t

if __name__ == '__main__':
    argc = len(sys.argv)
#    if argc <= 1:
#        raise Exception('no input param')
#    filename = sys.argv[1]
    
    with open(trainfile) as fr:
        data = fr.readlines()
    data = data[1:]
    uids = [line.split(',')[0] for line in data]
    skus = [line.split(',')[1] for line in data]
    raw_query = [line.split(',')[3] for line in data]
    click_t = [get_date(line.split(',')[4]) for line in data]
    query_t = [get_date(line.split(',')[5][0:-2]) for line in data]
    
    miss_iid = defaultdict(int)
    miss_word = defaultdict(lambda: defaultdict(int))
    miss_date = defaultdict(int)
    
    #filename = '/home/kingsfield/Desktop/BestBuyPc/popular_query_skus_train.csv'
    with open(filename) as fr:
        with open(difffile, 'w') as fw:
            if argc == 2:
                print 'no sku, assume the first line of file is not sku'
            else:
                print 'skip sku...'
                fr.readline()
            print 'begin to evaluate'
            idx = -1
            score = 0
            hit = 0
            hit_list = [0, 0, 0, 0, 0]
            while True:
                line = fr.readline()
                idx += 1
                if not line:
                    break
                line = line[0:-2]
                predict = line.split(' ')
                iid = skus[idx]
                if iid in predict:
                    index = predict.index(iid)
                    score += 1.0 / (index + 1)
                    hit += 1
                    hit_list[index] += 1
                    if index == 1:
                        outline = '%s,%s,%s,%s,%s,%s, %s' % (idx + 2, iid, uids[idx], raw_query[idx], ' '.join(Util.wordutil.get_correct_words(raw_query[idx])), click_t[idx], ','.join(predict))
                        print outline        
                else:
                    outline = '%s,%s,%s,%s,%s,%s, %s' % (idx + 2, iid, uids[idx], raw_query[idx], ' '.join(Util.wordutil.get_correct_words(raw_query[idx])), click_t[idx], ','.join(predict))
                    #print outline
                    fw.write(outline + '\n')
                    t = '%d%d' % (click_t[idx].month, click_t[idx].day)
#                    if iid == '2670133':
#                        print '%s,%s,%s,%s,%s' % (idx + 2, raw_query[idx], ' '.join(Util.wordutil.get_correct_words(raw_query[idx])), click_t[idx], ','.join(predict))
#                        miss_date[t] += 1
                    miss_iid[iid] += 1
                    words = Util.wordutil.get_correct_words(raw_query[idx])
                    for w in words:
                        miss_word[iid][w] += 1
        
#        hotest_miss = sorted(miss_iid.items(), key=lambda x:x[1], reverse=False)
#        for line in hotest_miss:
#            iid = line[0]
#            word_rank = sorted(miss_word[iid].items(), key=lambda x:x[1], reverse=True)
#            words = str(word_rank[0:5])
#            print '%s,%s %s' % (line[0], line[1], words)
                
        #hot_miss_day = sorted(miss_date.skus(), key=lambda x:x[0], reverse=False)
        #print 'miss data=%s' % (hot_miss_day)
        print 'miss=%s' % (sum(miss_iid.values()))
        print 'hit_list=%s' % (hit_list)
        print 'score=%s,hit=%s' % (score, hit)
        print 'map=%f' % (score * 1.0 / (idx + 1))
        print 'avg hit=%f' % (hit * 1.0 / (idx + 1))
